// 内容脚本 - 注入到网页中
console.log('Content script loaded on:', window.location.href);

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log('Content script received message:', request);
  
  if (request.type === 'GET_SELECTION') {
    // 获取用户选中的文本
    try {
      const selectedText = window.getSelection().toString().trim();
      console.log('Selected text:', selectedText);
      const response = { 
        success: true, 
        text: selectedText,
        hasSelection: selectedText.length > 0
      };
      console.log('Sending response:', response);
      sendResponse(response);
    } catch (error) {
      console.error('获取选中文本失败:', error);
      const errorResponse = { 
        success: false, 
        error: '获取选中文本失败',
        text: '',
        hasSelection: false
      };
      console.log('Sending error response:', errorResponse);
      sendResponse(errorResponse);
    }
  }
  
  if (request.type === 'GET_PAGE_CONTENT') {
    // 获取页面内容
    try {
      const pageContent = extractPageContent();
      console.log('Page content extracted:', pageContent);
      sendResponse({ 
        success: true, 
        content: pageContent
      });
    } catch (error) {
      console.error('获取页面内容失败:', error);
      sendResponse({ 
        success: false, 
        error: '获取页面内容失败',
        content: null
      });
    }
  }
  
  if (request.type === 'CLEAR_SELECTION') {
    // 清除页面选中状态
    try {
      // 清除页面选中状态
      if (window.getSelection) {
        window.getSelection().removeAllRanges();
      }
      // 清除document的选中状态
      if (document.getSelection) {
        document.getSelection().removeAllRanges();
      }
      console.log('页面选中状态已清除');
      sendResponse({ success: true });
    } catch (error) {
      console.error('清除选中状态失败:', error);
      sendResponse({ success: false, error: error.message });
    }
  }
});

// 提取页面内容的函数
function extractPageContent() {
  // 获取页面标题
  const title = document.title || '';
  
  // 获取主要内容
  let content = '';
  
  // 尝试获取主要内容区域
  const mainSelectors = [
    'main',
    'article',
    '.content',
    '.main-content',
    '#content',
    '#main',
    '.post-content',
    '.entry-content'
  ];
  
  let mainElement = null;
  for (const selector of mainSelectors) {
    mainElement = document.querySelector(selector);
    if (mainElement) break;
  }
  
  if (mainElement) {
    content = mainElement.innerText || mainElement.textContent || '';
  } else {
    // 如果没有找到主要内容区域，获取body内容
    const body = document.body;
    if (body) {
      // 移除脚本和样式标签
      const clone = body.cloneNode(true);
      const scripts = clone.querySelectorAll('script, style, nav, header, footer, .sidebar, .ad');
      scripts.forEach(el => el.remove());
      content = clone.innerText || clone.textContent || '';
    }
  }
  
  // 清理内容
  content = content.replace(/\s+/g, ' ').trim();
  
  // 限制内容长度
  if (content.length > 8000) {
    content = content.substring(0, 8000) + '...';
  }
  
  return {
    title: title,
    content: content,
    url: window.location.href
  };
} 